//	Copyright (C) 2007 Christian Martick, http://juhu-mac.blogspot.com
//
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.

#import "smcOverview.h"

extern NSString *kUIButtonBarButtonAction;
extern NSString *kUIButtonBarButtonInfo;
extern NSString *kUIButtonBarButtonInfoOffset;
extern NSString *kUIButtonBarButtonSelectedInfo;
extern NSString *kUIButtonBarButtonStyle;
extern NSString *kUIButtonBarButtonTag;
extern NSString *kUIButtonBarButtonTarget;
extern NSString *kUIButtonBarButtonTitle;
extern NSString *kUIButtonBarButtonTitleVerticalHeight;
extern NSString *kUIButtonBarButtonTitleWidth;
extern NSString *kUIButtonBarButtonType;

@implementation smcOverview

#pragma mark The usual

- (void) dealloc 
{	
	[theTable release];
	[topBar release];
	[_jaiku release];
	[super dealloc];
}

- initWithFrame: (CGRect) rect andDelegate: (id) obj
{
	self = [super initWithFrame: rect];
	if (self != nil)
	{
		_jaiku = nil;
		[self setDelegate: obj];
		
		// set color
		[self setBackgroundColor: [UIView colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha: 1.0f]];
	
		// add navigation bar
		topBar = [[UINavigationBar alloc] initWithFrame: 
			CGRectMake(0.0f, 0.0f, rect.size.width, 44.0f)];
		[topBar setBarStyle: 2];
		[topBar setDelegate: self];
		[topBar showLeftButton: [UIImage applicationImageNamed:@"write.png"] withStyle: 0
			rightButton: [UIImage applicationImageNamed:@"pref.png"] withStyle: 0];
		 
		// and a label
		UITextLabel *label = [[[UITextLabel alloc] initWithFrame: 
			CGRectMake(0.0f, 0.0f, rect.size.width, 44.0f)] autorelease];
		[label setText: @"juhu"];
		[label setBackgroundColor: [UIView colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha: 0.0f]];
		[label setColor: [UIView colorWithRed: 1.0f green: 1.0f	blue: 1.0f alpha: 1.0f]];
		[label setCentersHorizontally: YES];
		
		// the table
		theTable = [[UITable alloc] initWithFrame: 
			CGRectMake (0.0f, 0.0f, rect.size.width, rect.size.height)];
		
		UITableColumn *col = [[[UITableColumn alloc] initWithTitle: @"Hello"
			identifier: @"hello" width: [theTable frame].size.width] autorelease];
		
		[theTable addTableColumn: col]; 
		[theTable setShowScrollerIndicators: 0];
		[theTable setDataSource: self];
		[theTable setDelegate: self];
		[theTable enableRowDeletion: YES animated: YES];
		[theTable setBackgroundColor: [UIView colorWithRed: 0.0f green: 0.0f blue: 0.0f alpha: 0.0f]];
		[theTable setPadding: [topBar frame].size.height];
		[theTable reloadData];

		// add as subviews		
		[self addSubview: theTable];
		[self addSubview: topBar];
		[self addSubview: label];
	
		// button bar for refresh, calls directly [delegate update:]
		NSDictionary *refresh = [NSDictionary dictionaryWithObjectsAndKeys: 
			@"refresh.png", kUIButtonBarButtonInfo, 
			[NSNumber numberWithUnsignedInt: 1], kUIButtonBarButtonTag, 
			[self delegate], kUIButtonBarButtonTarget,
			@"update:", kUIButtonBarButtonAction, nil];

		NSArray *items = [NSArray arrayWithObjects: refresh, nil];
		UIButtonBar *buttonBar = [[[UIButtonBar alloc] initInView: self 
			withFrame: CGRectMake(0.0f, [self frame].size.height - 30.0f, [self frame].size.width, 30.0f) withItemList: items] autorelease];
		[buttonBar setBarStyle: 2];

		int buttons[1] = {1};
		[buttonBar registerButtonGroup: 1 withButtons: buttons withCount: 1];
		[buttonBar showButtonGroup: 1 withDuration:0.0f];
	}
	
	return self;
}

#pragma mark Set, get and action

- (void) showTop
{
	[theTable scrollRowToVisible: 0];
}

- (int) selected
{
	return [theTable selectedRow];
}

- (void) unselect
{
	// well, kind of...
	[theTable selectRow: -1 byExtendingSelection: NO withFade: YES];
}

- (void) reload
{
	[theTable reloadData];
}

- (void) setJaikuObject: (smcJaikuAPI *) obj
{
	if (_jaiku != obj)
	{
		[_jaiku release];
		_jaiku = [obj retain];
	}
}

#pragma mark Navigation bar delegate

- (void)navigationBar: (UINavigationBar*) bar buttonClicked: (int) button 
{
	if (bar == topBar)
		switch (button)
		{
			case 0:
				// show settings
				if ([[self delegate] respondsToSelector: @selector(animateFromTo:)])
					[[self delegate] performSelector: @selector(animateFromTo:) withObject: 
						[NSArray arrayWithObjects: self, [NSNumber numberWithInt: 2], nil]];
				break;
			case 1:
				// show write view aka mainview
				if ([[self delegate] respondsToSelector: @selector(animateFromTo:)])
					[[self delegate] performSelector: @selector(animateFromTo:) withObject: 
						[NSArray arrayWithObjects: self, [NSNumber numberWithInt: 1], nil]];
				break;
		}
}

#pragma mark Table data source & delegate

- (float) table: (UITable *) table heightForRow: (int) row
{	
	return 142.0f;
}

- (int) numberOfRowsInTable: (UITable *)table
{
    return [[_jaiku stream: [[NSUserDefaults standardUserDefaults] stringForKey: @"stream"]] count];
}

- (UITableCell *) table: (UITable *)table cellForRow: (int)row column: (int)col
{
	NSDictionary *mh = [[_jaiku stream: [[NSUserDefaults standardUserDefaults] stringForKey: @"stream"]] objectAtIndex: row];
	
	smcTableCell *aCell = [[[smcTableCell alloc] initWithFrame: 
		CGRectMake (0.0f, 0.0f, [table frame].size.width, 142.0f)] autorelease];
	
	// if comment, no loction
	NSString *locationStr = nil;
	if ([[[mh objectForKey: kSmcEntryKeyURL] absoluteString] rangeOfString: @"#c-"].location != NSNotFound)
		locationStr = @""; // used to be 'comment' but kind of unnecessary..
	else
		locationStr = [mh objectForKey: kSmcEntryKeyLocation];
	
	[aCell setContent: [NSDictionary dictionaryWithObjectsAndKeys:
		[NSString stringWithFormat: @"%@ (%@)", [mh objectForKey: kSmcEntryKeyFirstName], 
			[mh objectForKey: kSmcEntryKeyRelativeDateString]], @"name",
		[mh objectForKey: kSmcEntryKeyTitle], @"text",
		locationStr, @"location",
		[mh objectForKey: kSmcEntryKeyUserImage], @"image", nil]];
	
	return aCell; 
}

- (void)tableRowSelected: (NSNotification*) notification
{
	// only call if real selection and not unselect
	if (([[self delegate] respondsToSelector: @selector(animateFromTo:)]) && 
		([[_jaiku stream: [[NSUserDefaults standardUserDefaults] objectForKey: @"stream"]] count] > [theTable selectedRow]))
	{ //NSLog(@"calling home %i", [theTable selectedRow]);
			[[self delegate] performSelector: @selector(animateFromTo:) withObject: 
				[NSArray arrayWithObjects: self, [NSNumber numberWithInt: 3], nil]];
	}
}

@end
